package com.definesys.smecrailtransit.module.TotalTime.dialog;

import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.api.SmecRailConstant;
import com.definesys.smecrailtransit.databinding.DialogTotalTimeBinding;
import com.definesys.smecrailtransit.module.TotalTime.PopWindowAdapter.PopDateTimeAdapter;
import com.definesys.smecrailtransit.module.TotalTime.PopWindowAdapter.PopWindowAdapter;
import com.definesys.smecrailtransit.module.TotalTime.TotalTimePresenter;
import com.definesys.smecrailtransit.module.TotalTime.bean.DateTimeBean;
import com.definesys.smecrailtransit.module.TotalTime.bean.ElevatorBean;
import com.definesys.smecrailtransit.utils.CommonUtils;
import com.definesys.smecrailtransit.utils.ToastyUtils;
import com.hwangjr.rxbus.Bus;
import com.hwangjr.rxbus.SmecRxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;


/**
 * Created by cjy on 2017/9/3.
 */

public class TotalTimeDialog extends Dialog {
    public static final String GET_STATION_TIME_SUCCESS = "getStationTimeSuccess(Object o)";
    public static final String GET_STATION_TIME_FAILED = "getStationTimeFailed(Object o)";
    public static final String SHOW_ELEVATOR_TIME = "showElevatorTime(Object o)";
    public static final String GET_ELEVATOR_TIME_SUCCESS = "getElevatorTimeSuccess(Object o)";
    public static final String GET_ELEVATOR_TIME_FAILED = "getElevatorTimeFailed(Object o)";

    private PopWindowAdapter popWindowAdapter;
    private PopDateTimeAdapter popDateTimeAdapter;
    private DisplayMetrics dm;
    private DialogTotalTimeBinding dialogTotalTimeBinding;
    private TotalTimePresenter totalTimePresenter;
    private Context mContext;
    protected Bus mRxBus;

    private String selectMonth;
    private String selectYear;
    private String eleContractNo;
    private HashMap<String,Object> result;

    public TotalTimeDialog(Context context, int themeResId, String selectMonth, String selectYear,HashMap<String,Object> o) {
        super(context, themeResId);
        this.mContext = context;
        mRxBus= SmecRxBus.get();
        mRxBus.register(this);
        dm = mContext.getResources().getDisplayMetrics();
        totalTimePresenter = new TotalTimePresenter(mContext);
        this.selectMonth = selectMonth;
        this.selectYear = selectYear;
        result = o;

        dialogTotalTimeBinding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.dialog_total_time,null ,false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(dialogTotalTimeBinding.getRoot());
        _initView();
    }

    private void _initView(){
        popWindowAdapter = new PopWindowAdapter(this.dm,this.mRxBus);
        popDateTimeAdapter = new PopDateTimeAdapter(this.dm);

        //弹出框
        dialogTotalTimeBinding.popWindowContent.setLayoutManager(new LinearLayoutManager(mContext));
        dialogTotalTimeBinding.popWindowContent.setAdapter(popWindowAdapter);
        //电梯日度运行时长弹出框
        dialogTotalTimeBinding.dateTimeList.setLayoutManager(new LinearLayoutManager(mContext));
        dialogTotalTimeBinding.dateTimeList.setAdapter(popDateTimeAdapter);

        //卡片翻转
        CommonUtils.setCameraDistance(mContext.getResources().getDisplayMetrics(),dialogTotalTimeBinding.card);
        dialogTotalTimeBinding.popWindowDetailBack.setOnClickListener(v ->{
            CommonUtils.cardFront(dialogTotalTimeBinding.card, R.id.popWindow, R.id.popWindowDetail);

            dialogTotalTimeBinding.getRoot().postDelayed(new Runnable() {
                @Override
                public void run() {
                    dialogTotalTimeBinding.refreshStationTime.startRefresh();

                    popDateTimeAdapter.start = 0;
                    popDateTimeAdapter.setList(new ArrayList<>());
                    popDateTimeAdapter.notifyDataSetChanged();
                }
            },800);
        });

        dialogTotalTimeBinding.popWindowTitle.setText((String)result.get("stationName"));
        
        /*******下拉刷新设置********/
        //站点运行时长下拉刷新
        dialogTotalTimeBinding.refreshStationTime.setHeaderView(SmecRailConstant.getSinaRefreshView());
        dialogTotalTimeBinding.refreshStationTime.setAutoLoadMore(true);
        dialogTotalTimeBinding.refreshStationTime.setEnableOverScroll(false);
        dialogTotalTimeBinding.refreshStationTime.setEnableLoadmore(false);
        dialogTotalTimeBinding.refreshStationTime.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                getStationTime((int)result.get("stationId"));
            }
        });
        //电梯月度运行时长下拉刷新
        dialogTotalTimeBinding.refreshElevatorTime.setHeaderView(SmecRailConstant.getSinaRefreshView());
        dialogTotalTimeBinding.refreshElevatorTime.setAutoLoadMore(true);
        dialogTotalTimeBinding.refreshElevatorTime.setEnableOverScroll(false);
        dialogTotalTimeBinding.refreshElevatorTime.setEnableLoadmore(false);
        dialogTotalTimeBinding.refreshElevatorTime.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                getElevatorTime(eleContractNo);
            }
        });

        dialogTotalTimeBinding.refreshStationTime.startRefresh();

    }


    /**
     * 获取站点运行时长成功
     */
    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_STATION_TIME_SUCCESS)
            }
    )
    public void getStationTimeSuccess(ArrayList<ElevatorBean> list){
        popWindowAdapter.showAnimation = true;
        popWindowAdapter.setList(list);
        popWindowAdapter.notifyDataSetChanged();
        dialogTotalTimeBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                popWindowAdapter.showAnimation = false;
            }
        },500);
        dialogTotalTimeBinding.refreshStationTime.finishRefreshing();
    }

    /**
     * 获取站点运行时长失败
     */
    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_STATION_TIME_FAILED)
            }
    )
    public void getStationTimeFailed(Object o){
        popWindowAdapter.showAnimation = true;
        popWindowAdapter.setList(new ArrayList<>());
        popWindowAdapter.notifyDataSetChanged();
        dialogTotalTimeBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                popWindowAdapter.showAnimation = false;
            }
        },500);
        dialogTotalTimeBinding.refreshStationTime.finishRefreshing();
        //ToastyUtils.showWaringToast(dialogTotalTimeBinding.getRoot().getContext(),"获取获取站点运行时长失败");
    }

    /**
     * 展示电梯运行时长弹出框
     */
    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(SHOW_ELEVATOR_TIME)
            }
    )
    public void showElevatorTime(HashMap<String,String> o){
        this.eleContractNo = o.get("eleContractNo");
        dialogTotalTimeBinding.popWindowDetailTitle.setText(o.get("eleCode"));
        CommonUtils.cardBack(dialogTotalTimeBinding.card, dialogTotalTimeBinding.popWindowDetail, R.id.popWindow, R.id.popWindowDetail);

        dialogTotalTimeBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                dialogTotalTimeBinding.refreshElevatorTime.startRefresh();

                popWindowAdapter.setList(new ArrayList<>());
                popWindowAdapter.notifyDataSetChanged();
            }
        },800);
    }

    /**
     * 获取电梯运行时长成功
     */
    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_ELEVATOR_TIME_SUCCESS)
            }
    )
    public void getElevatorTimeSuccess(ArrayList<DateTimeBean> list){
        popDateTimeAdapter.showAnimation = true;
        popDateTimeAdapter.setList(list);
        popDateTimeAdapter.notifyDataSetChanged();
        dialogTotalTimeBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                popDateTimeAdapter.showAnimation = false;
            }
        },500);
        dialogTotalTimeBinding.refreshElevatorTime.finishRefreshing();
    }

    /**
     * 获取电梯运行时长失败
     */
    @Subscribe(

            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_ELEVATOR_TIME_FAILED)
            }
    )
    public void getElevatorTimeFailed(Object o){
        popDateTimeAdapter.showAnimation = true;
        popDateTimeAdapter.setList(new ArrayList<>());
        popDateTimeAdapter.notifyDataSetChanged();
        dialogTotalTimeBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                popDateTimeAdapter.showAnimation = false;
            }
        },500);
        dialogTotalTimeBinding.refreshElevatorTime.finishRefreshing();
        ToastyUtils.showWaringToast(dialogTotalTimeBinding.getRoot().getContext(),"获取获取电梯运行时长失败");
    }

    //获取站点运行时长
    public void getStationTime(int stationId){
        //设置参数
        HashMap<String, Object> param = new HashMap<>();

        param.put("stationId",stationId);

        if(!selectMonth.equals("10") || !selectMonth.equals("11") || !selectMonth.equals("12")){
            param.put("date",selectYear + "-0" + selectMonth);
        }else{
            param.put("date",selectYear + "-" + selectMonth);
        }

        //网络请求
        this.totalTimePresenter.getStationTime(param);
    }

    //获取电梯运行时长
    public void getElevatorTime(String eleContractNo){
        popDateTimeAdapter.start =1;
        //设置参数
        HashMap<String, Object> param = new HashMap<>();

        param.put("eleContractNo",eleContractNo);

        if(!selectMonth.equals("10") || !selectMonth.equals("11") || !selectMonth.equals("12")){
            param.put("date",selectYear + "-0" + selectMonth);
        }else{
            param.put("date",selectYear + "-" + selectMonth);
        }

        //网络请求
        this.totalTimePresenter.getElevatorTime(param);
    }

}
